package cn.cai.pethome.pet.controller;

import cn.cai.pethome.base.PreAuthorize;
import cn.cai.pethome.base.constant.Constant;
import cn.cai.pethome.base.dto.LogininfoDto;
import cn.cai.pethome.base.util.UserContext;
import cn.cai.pethome.pet.dto.PetOnlineAuditDto;
import cn.cai.pethome.pet.service.IPetService;
import cn.cai.pethome.pet.domain.Pet;
import cn.cai.pethome.pet.query.PetQuery;
import cn.cai.pethome.base.util.AjaxResult;
import cn.cai.pethome.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;

    @Autowired
    private UserContext userContext;

    /**
     * 宠物上架申请店铺员工使用
     * @param petIds
     * @return
     */
    @PreAuthorize(name = "宠物上架申请",sn = "pet:onsale",parent_id = 11)
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> petIds){

        try {
            petService.onsale(petIds);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("宠物上架申请失败！" + e.getMessage());
        }
    }


    /**
     * 根据宠物id查询宠物的图片集合地址和宠物详情数据
     * @param peiId
     * @return
     */
    @GetMapping("/getIMagesAndDetail/{peiId}")
    public Map<String,Object> getIMagesAndDetail(@PathVariable("peiId")Long peiId){
        return petService.getIMagesAndDetail(peiId);
    }

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */

    @PreAuthorize(name = "新增或者修改宠物",sn = "pet:save",parent_id = 11)
    @PutMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        LogininfoDto logininfoDto = userContext.getUser(request.getHeader(Constant.CURRENT_USER_HEADER_NAME));

        try {
            pet.setShop_id(logininfoDto.getAdmin().getShop_id());
            if( pet.getId()!=null){
                    petService.update(pet);
            }
            else{
                    petService.add(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    public List<Pet> list(){
        return petService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PreAuthorize(name = "宠物管理", sn = "pet:pagelist",menu_id = 12,parent_id = 11)
    @PostMapping("/pagelist")
    public PageList<Pet> json(@RequestBody PetQuery query,HttpServletRequest request)
    { LogininfoDto logininfoDto = userContext.getUser(request.getHeader(Constant.CURRENT_USER_HEADER_NAME));
        if(logininfoDto.getLogininfo().getType() == 0){
            //只有当前登录用户属于店铺员工时，
            query.setShop_id(logininfoDto.getAdmin().getShop_id());
        }
        return petService.queryAll(query);
    }

    /**
     * 提交宠物上架审核结果
     * @param petOnlineAuditDto
     * @return
     */
    @PreAuthorize(name = "提交宠物上架审核结果",sn = "pet:petOnlineApplyAudit",parent_id = 11)
    @PostMapping("/petOnlineApplyAudit")
    public AjaxResult petOnlineApplyAudit(@RequestBody PetOnlineAuditDto petOnlineAuditDto,HttpServletRequest request){

        try {
            //获取当前登录用户信息
            LogininfoDto logininfoDto = userContext.getUser(request.getHeader(Constant.CURRENT_USER_HEADER_NAME));
            petOnlineAuditDto.setLogininfoId(logininfoDto.getLogininfo().getId());

            petService.petOnlineApplyAudit(petOnlineAuditDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("宠物上架审核结果失败！" + e.getMessage());
        }
    }

}
